package com.atguigu.search.thread;

import java.util.concurrent.*;

/**
 * @author xyouzi
 * @date 2022/7/27 16:15
 */
public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("start");
//        CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程："+Thread.currentThread().getId());
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//        },executor);

//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor);
////        Integer integer = integerCompletableFuture.get();
//        integerCompletableFuture.whenComplete((result,exception)->{
//            System.out.println("异步任务完成。。结果是：" + result + ";异常是"+exception);
//        });
//        CompletableFuture<Integer> exceptionally = integerCompletableFuture.exceptionally(throwable -> {
//            System.out.println("异常是：" + throwable);
//            return 10;
//        });
//        Integer integer = exceptionally.get();
//        System.out.println(integer);

        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("1");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "world";
        });

        System.out.println(future1.get());
        System.out.println(future2.get());
        System.out.println("end");
    }
}
